/*公共函数库*/


/*XML转数组*/ //TODO 有没有更好的方法
function xml2arr(xml){
  var $xml = xml.jquery?xml.children():$(xml).children();
  var arr = {};
  for (var i = 0; i < $xml.length; i++) {
    var $children = $($xml[i]).children();
    if ($children.length){
      arr[$xml[i].tagName] = xml2arr($xml[i])
    }else{
      arr[$xml[i].tagName] = $($xml[i]).text();
    }
  };
  return arr;
}

/*显示桌面消息*/
function notify(e){
    if (window.webkitNotifications) {
        if (window.webkitNotifications.checkPermission() == 0) {
            var popup = window.webkitNotifications.createNotification(e.icon, e.title, e.body);
            console.log(popup);
            popup.onclick = function(){
              if(e.url)window.open(e.url,"_blank");
            }
            popup.ondisplay = function(event) {
                setTimeout(function() {
                    event.currentTarget.cancel();
                }, AUTO_CLOSE_DELAY_SECONDS * 1000);
            }
            if (("show" in popup)){
              popup.show();
            }
        }else{
            window.webkitNotifications.requestPermission();
            return;
        }
    }
}

console.log();

/*异步*/
function asynchronous(fun1,fun2){
  setTimeout(function(){
    try{
      if (isFunc(fun1)){
        var return1 = fun1();
        if (isFunc(fun2)){
          fun2(return1);
        }
      }
    }catch(e){
      console.error(e)
    }
  },100);
}

/*类型判断*/
window.isEmpty = function(obj) {
  return (obj == null || typeof obj == "undefined" || obj.length == 0)
}

window.isNotEmpty = function(obj) {
  return !isEmpty(obj);
}

window.isFunc = function(fun) {
  return (fun != null && typeof fun == "function");
}

window.isNotFunc = function(fun) {
  return !isFunc(fun);
}

window.typeOf = function(cur, type) {
  if (typeof type != "string")
    return false;
  return typeof cur == type;
}

window.isArray = function(array) {
  return isNotEmpty(array) && className(array) == "Array"
}

window.isNotArray = function(arr) {
  return !isArray(arr);
}

window.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*模板引擎 http://www.oschina.net/code/snippet_919901_26970*/
(function (window) {
  function Template(str) {
    this.str = str;
  }
  Template.prototype.format = function () {
    var arg = arguments[0] instanceof Array ? arguments[0] : arguments;
    return this.str.replace(/\{(\d+)\}/g, function (a, b) {
      return arg[b] || '';
    });
  }
  window.Template = Template;
})(window);

/*注释转字符串*/
function hereDoc(f){return f.toString().replace(/^[^\/]+\/\*!?\s?/,'').replace(/\*\/[^\/]+$/,'');}